package org.android.tools;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GpsLocation {

	private Activity activity = null;
	private LocationManager mlocManager = null;
	private LocationListener mlocListener = null;
	public GpsLocation(Activity activity){

		this.activity = activity;

		mlocManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE); 
		mlocListener = new MyLocationListener ();
		mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
		
	}

	public Double[] getGPS() {

		List<String> providers = mlocManager.getProviders(true);

		/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
		Location l = null;

		for (int i=providers.size()-1; i>=0; i--) {
			l = mlocManager.getLastKnownLocation(providers.get(i));
			if (l != null) break;
		}

		Double[] gps = new Double[2];
		if (l != null) {
			gps[0] = l.getLatitude();
			gps[1] = l.getLongitude();
		}
		return gps;
	}

	public LocationManager getMlocManager() {
		return mlocManager;
	}

	public class MyLocationListener implements LocationListener

	{

		public void onLocationChanged(Location loc)

		{

			loc.getLatitude();

			loc.getLongitude();

			String Text = "My current location is: " + "Latitud = " + loc.getLatitude() +

					"Longitud = " + loc.getLongitude(); 



		}	




		public void onProviderDisabled(String provider)

		{
			//Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();

		}


		public void onProviderEnabled(String provider) {

			//Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();

		}


		public void onStatusChanged(String provider, int status, Bundle extras)

		{


		}








	}
}
